home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Int to Double, Pl Advise!
- Date: Fri, 29 Mar 96 12:38:52 GMT
- Organization: none
- Message-ID: <828103132snz@genesis.demon.co.uk>
- References: <isa5224.544.315B647A@age2.age.uiuc.edu>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <isa5224.544.315B647A@age2.age.uiuc.edu>
- isa5224@age2.age.uiuc.edu "Irfan S. Ahmad" writes:
-
- >Hi:
- >
- >I am having problems with the following type conversion. From int to double.
- >If someone can comment:
- >
- >#define Ng 4
- >#define Nr 4
- >
- >#include <stdio.h>
- >#include <math.h>
- >#include <malloc.h>
-
- None of these header files are actually needed here. malloc.h is not a
- standard header file - for malloc() and related functions you should
- include stdlib.h.
-
- >int p0[Ng][Nr];
- >
- >int main(){
- >int i,j,k;
- >float x =0;
- >float y =0;
- >:
- >:
- >:
- >for (i=1; i<Ng; i++) {
- > for (j=1; j<Nr; j++) {
- >
- > x = x + ((double) p0[i,j]) / (double)(j*j); }
-
- The expression (i,j) evaluates i, discards its result evaluates j and uses
- the result as the value of the expression. So in this case p0[i,j] is
- equivalent to p0[j] which is an array of 4 ints. As the compiler correctly
- diagnoses you can't convert from an array to a double.
-
- C doesn't have multi-dimensional arrays although you can build arrays of
- arrays. You must dereference them appropriately i.e.
-
- p0[i][j]
-
- >}
- >
- >I get the following compilation error(s) on MS C++ compiler:
- >error C2440: 'cast' : cannot convert from 'int [4]' to 'double '
-
- Make sure you use a compiler appropriate to the language you are writing in.
- Since you posted to comp.lang.c I assume that is C. Most C++ compilers have
- an ANSI C mode - make sure you use it to compile C code.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-